// pages/group/group.js
const group = require("../../utils/group");
var g = require("../../utils/group")
Page({
  groupname:function(e){
      this.setData({
        groupname:e.detail.value
      });
  },
  groupnum:function(e){
    this.setData({
      groupnum:e.detail.value
    });
},

creategroup(){
   var database =  wx.cloud.database();
   var groupname = this.data.groupname;
   var groupnum = this.data.groupnum;
  database.collection("tb_group").add({
     data:{
        groupname:groupname,
        groupnum:groupnum
  }}).then((res)=>{
    if(res)wx.showToast({
      title: '创建小组成功',
    })
  })
},
  Kaishi(){
    var database = wx.cloud.database();
    var k = this.data.currentIndex;
    var matchid = this.data.result[k]._id;
    database.collection("tb_match").doc(matchid).update({
      data:{
        beginTime:new Date()
      }
    }).then((res)=>{
      if(res)wx.showToast({
        title: '比赛开始成功',
      })
    })
  },
  canjia(){
    var userid = this.data.userid;
    var username = this.data.username;
    var k = this.data.index;
    var matchid = this.data.result[k]._id;
    var matchname = this.data.result[k].title;
    var database = wx.cloud.database();
    database.collection("tb_baoming").add({
      data:{
        userid:userid,
        username:username,
        matchid:matchid,
        matchname:matchname
      }
    }).then((res)=>{
      if(res)wx.showToast({
        title: '报名成功',
      })
    }).catch((err)=>{console.log(err)});
  },
  changeCategory:function(e){
      this.setData({
        category:e.currentTarget.id
      });
  },
  shuru:function(e){
     this.setData({
       neirong:e.detail.value
     });
  },
  rudui(){
      var k = this.data.index;
      var groupid = this.data.result[k]._id;
      var groupname = this.data.result[k].groupname;
      var userid = this.data.userid;
      var username = this.data.username;
      var database = wx.cloud.database();
      database.collection("tb_rudui").add({
        data:{
          groupid:groupid,
          groupname:groupname,
          userid:userid,
          username:username
        }
      }).then((res)=>{
        if(res)wx.showToast({
          title: '加入小队成功',
        })
      })
  },
  click(){
   var database = wx.cloud.database();
 var that = this;
   database.collection("tb_group").where({}).get().then((res)=>{
        var data = res.data;
        var result = [];


        for(let i=0;i<data.length;i++){
            if(data[i].groupname.match(that.data.neirong)){
               var d = data[i];
               result.push(d);
            }
        }
            that.setData({
              result:result
              });
   });
  },
  dianji:function(e){
       this.setData({
         index:e.currentTarget.id,
         flag:true
       });
        var k = this.data.index;
        var groupid = this.data.result[k]._id;
        var database = wx.cloud.database();
        var that = this;
        database.collection("tb_rudui").where({
          groupid:groupid
        }).get().then((res)=>{
          that.setData({
            zuyuan:res.data
          });
        });
  },
  sousuo(){
    var neirong = this.data.neirong;
    var database = wx.cloud.database();
    database.collection("tb_match").where({}).get().then((res)=>{
      var matches = res.data;
      var result = [];

      for(let i=0;i<matches.length;i++){
        var data = matches[i];
        if(data.title.match(neirong)){
          result.push(data);
        }
      }
      this.setData({
        result:result
      });
    }).catch((err)=>{
      console.log(err);
    });

  },
  cansai:function(e){
    var that = this;
    var k = e.currentTarget.id;
    this.setData({
      currentIndex:k
    })
    var matchid = this.data.result[k]._id;
    var userid = this.data.userid;
    var database = wx.cloud.database();
    database.collection("tb_baoming").where({
      matchid:matchid
    }).get().then((res)=>{
      that.setData({
      xuanshou:res.data
      });
    }).catch((err)=>{console.log(err)});
  this.setData({
    index:e.currentTarget.id,
    biaozhi:true
  });
  //判断当前用户是否为当前比赛的创建者
  that.setData({
    isKaishi:false
  });
  var chuangjianzhe = that.data.result[k].userid;
  if(that.data.userid == chuangjianzhe ){
    that.setData({
      isKaishi:true
    });
  }else{
    that.setData({
      isKaishi:false
    });
  }
  },
  shangchuang(){
    var that = this;
    wx.chooseMedia({
      count: 1,
      success(res) {
          console.log(res.tempFiles)
          var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
          var ext = res.tempFiles[0].tempFilePath.slice(po)
          wx.cloud.uploadFile({
              cloudPath: that.data.picname + ext,
              filePath: res.tempFiles[0].tempFilePath,
              success(res) {
                  console.log(res.fileID)
                  that.data.imageList.push(res.fileID)
                  that.setData({
                    imageList:that.data.imageList
                  });
              }
          })
      }
  })
  },
  matchname:function(e){
    this.setData({
      matchname:e.detail.value
    });
  },
  picname:function(e){
    this.setData({
      picname:e.detail.value
    });
  },


  delete(){
  console.log(this.data.currentIndex);
   this.data.imageList.splice(this.data.currentIndex,1)
   this.setData({
    imageList:this.data.imageList,
     flag:false
   });

  },


  choose:function(e){
   
    var k = e.currentTarget.id;
   
    this.setData({
      flag3:true,
      currentImage:this.data.imageList[k],
      currentIndex:k
    });
  },
    /**
     * 页面的初始数据
     */
    data: {
      zuyuan:[],
      groupname:null,
      groupnum:0,
      isKaishi:true,
     xuanshou:[],
      currentIndex:0,
      flag3:false,
      currentImages:null,
   imageList:[],
   picname:null,
   titles:g.list,
   category:2,
   neirong:null,
   result:[],
   index:0,
   flag:false,
   biaozhi:false,
   matchname:null,
   userid:null,
   username:null

    },
  create(){
    var that = this;
    var database = wx.cloud.database();
    var data = {
      title:this.data.matchname,
      userid:this.data.userid 
    };
    database.collection("tb_match").add({
      data:{
        title:that.data.matchname,
        userid:that.data.userid ,
        imageList:that.data.imageList
      }
    }).then((res)=>{
      console.log("插入一条数据后数据库返回的数据:"+res);
      if(res){
        wx.showToast({
          title: '成功插入数据',
        })
      }else{
        wx.showToast({
          title: '插入数据失败',
        })
      }
    }).catch((err)=>{
      console.log(err);
    });
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

      var that = this;
       wx.getStorage({
         key:"userid",
         success(res){
          that.setData({
            userid:res.data
          });
          //找到userid对应的用户名username
          var database = wx.cloud.database();
          database.collection("tb_user").where({
            _id:that.data.userid
          }).get().then((msg)=>{
            that.setData({
              username:msg.data[0].name
            })
          }).catch((err)=>{console.log(err)});
         }
       })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})